// 类的多态案例3 - 电脑组装
#include <iostream>
using namespace std;

// 抽象cpu类
class CPU
{

public:
    virtual void calculate() = 0; // 计算虚函数
};

// 抽象显卡类
class VideoCard
{
public:
    virtual void dispaly() = 0; // 显示虚函数
};

// 抽象内存条类
class Memory
{
public:
    virtual void storage() = 0; // 存储虚函数
};

class Computer
{
public:
    Computer(CPU *cpu, VideoCard *video_card, Memory *memory)
    {
        m_cpu = cpu;
        m_video_card = video_card;
        m_memory = memory;
    }

    void work()
    {
        m_cpu->calculate();
        m_video_card->dispaly();
        m_memory->storage();
    }

    ~Computer()
    {
        cout << "release computer..." << endl;
        // # 释放cpu
        if (m_cpu != NULL)
        {
            delete m_cpu;
            m_cpu = NULL;
        }

        // 释放显卡
        if (m_video_card != NULL)
        {
            delete m_video_card;
            m_video_card = NULL;
        }

        // 释放内存
        if (m_memory != NULL)
        {
            delete m_memory;
            m_memory = NULL;
        }
    }

private:
    CPU *m_cpu;
    VideoCard *m_video_card;
    Memory *m_memory;
};

// 具体厂商
class IntelCPU : public CPU
{
public:
    void calculate()
    {
        cout << "Intel cpu begin calculate..." << endl;
    }
};

class IntelVideoCard : public VideoCard
{
public:
    void dispaly()
    {
        cout << "Intel videocard is displaying..." << endl;
    }
};

class IntelMemory : public Memory
{
public:
    void storage()
    {
        cout << "Intel memory is stroaging..." << endl;
    }
};

class AMDCPU : public CPU
{
public:
    void calculate()
    {
        cout << "AMD cpu begin calculate..." << endl;
    }
};

class AMDVideoCard : public VideoCard
{
public:
    void dispaly()
    {
        cout << "AMD videocard is displaying..." << endl;
    }
};

class AMDMemory : public Memory
{
public:
    void storage()
    {
        cout << "AMD memory is stroaging..." << endl;
    }
};


int main()
{
    // 组装第一台电脑
    CPU *intel_cpu = new IntelCPU;
    VideoCard *intel_videocard = new IntelVideoCard;
    Memory * intel_memory = new IntelMemory;
    // Computer computer1(intel_cpu, intel_videocard, intel_memory);
    Computer *computer1 = new Computer(intel_cpu, intel_videocard, intel_memory);
    cout << "-> The 1st computer working..." << endl;
    // computer1.work();
    computer1->work();
    delete computer1;

    cout << "-----------------------" << endl;

    // 组装第二台电脑
    Computer *computer2 = new Computer(new AMDCPU, new AMDVideoCard, new AMDMemory);
    cout << "-> The 2nd computer working..." << endl;
    computer2->work();
    delete computer2;

    cout << "-----------------------" << endl;
    
    // 组装第三台电脑
    Computer *computer3 = new Computer(new AMDCPU, new IntelVideoCard, new AMDMemory);
    cout << "-> The 3rd computer working..." << endl;
    computer3->work();
    delete computer3;

    return 0;
}